package com.heima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.PageResponse;
import com.heima.mapper.SkuClassMapper;
import com.heima.entity.SkuClass;
import com.heima.service.SkuClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SkuClassServiceImpl extends ServiceImpl<SkuClassMapper,SkuClass> implements SkuClassService {

    @Autowired
    private SkuClassMapper skuClassMapper;
//
//    @Autowired
//    private ClassifyService classifyService;

    @Override
    public Boolean add(SkuClass dto) {
        int row = skuClassMapper.insert(dto);
        if(row>0){
            return true;
        }
        return false;
    }

    /**
     * 分类搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param className
     * @return
     */
    @Override
    public PageResponse listPage(Integer pageIndex, Integer pageSize, String className) {
        // 构造分页对象
        Page<SkuClass> page = new Page<>(pageIndex, pageSize);

        // 构造查询条件
        QueryWrapper<SkuClass> wrapper = new QueryWrapper<>();
        if (className != null) {
            wrapper.like("class_name", className);
        }

        try {
            // 执行分页查询
            this.page(page, wrapper);

            // 构造返回结果
            PageResponse pageResponse = new PageResponse();
            pageResponse.setPageIndex((int) page.getCurrent());
            pageResponse.setPageSize((int) page.getSize());
            pageResponse.setTotalCount((int) page.getTotal());
            pageResponse.setCurrentPageRecords(page.getRecords());

            return pageResponse;
        } catch (Exception e) {
            // 添加日志记录或其他错误处理逻辑
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取分类基本信息
     *
     * @param clazzId
     * @return
     */
    @Override
    public SkuClass getClazzById(String clazzId) {
        SkuClass skuClass = skuClassMapper.selectById(Integer.parseInt(clazzId));
        return skuClass;
    }

    /**
     * 修改分类
     *
     * @param classId
     * @param className
     * @return
     */
    @Override
    public Boolean updateSku(String classId, String className) {
        SkuClass skuClass = new SkuClass();
        skuClass.setClassId(Integer.parseInt(classId));
        skuClass.setClassName(className);
        int row = skuClassMapper.updateById(skuClass);
        if (row > 0) {
            return true;
        }
        return false;
    }

    /**
     * 删除分类
     *
     * @param classId
     */
    @Override
    public void deleteSku(String classId) {
        skuClassMapper.deleteById(Integer.parseInt(classId));
    }
}
